﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace gdv
{
    public static class DiezteAndreasOrbit
    {
        static VertexBuffer vertBuffer;
        static BasicEffect effect;
        static int sphereResolution;

        public static void InitializeGraphics(GraphicsDevice graphicsDevice, int sphereResolution)
        {
            DiezteAndreasOrbit.sphereResolution = sphereResolution;

            effect = new BasicEffect(graphicsDevice);
            effect.LightingEnabled = false;
            effect.VertexColorEnabled = false;

            VertexPositionColor[] verts = new VertexPositionColor[(sphereResolution + 1) * 3];

            int index = 0;

            float step = MathHelper.TwoPi / (float)sphereResolution;

            // XZ
            for (float a = 0.0f; a <= MathHelper.TwoPi; a += step)
            {
                verts[index++] = new VertexPositionColor(new Vector3((float)Math.Cos(a), 0.0f, (float)Math.Sin(a)), Color.White);
            }

            vertBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), verts.Length, BufferUsage.None);
            vertBuffer.SetData(verts);
        }

        public static void Render(Vector3 size, Vector3 pos, GraphicsDevice graphicsDevice, Matrix view, Matrix projection, Color color)
        {
            if (vertBuffer == null)
                InitializeGraphics(graphicsDevice, 600);

            graphicsDevice.SetVertexBuffer(vertBuffer);
            effect.World = Matrix.CreateScale(size) * Matrix.CreateTranslation(pos);
            effect.View = view;
            effect.Projection = projection;
            effect.DiffuseColor = color.ToVector3();

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawPrimitives(PrimitiveType.LineStrip, 0, sphereResolution);
            }
        }
    }
}
